package wsclients;

import java.util.List;

import org.apache.commons.httpclient.protocol.Protocol;
import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
import org.codehaus.xfire.MessageContext;
import org.codehaus.xfire.client.Client;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.handler.AbstractHandler;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
import org.codehaus.xfire.transport.http.EasySSLProtocolSocketFactory;
import org.jdom.Element;
import org.jdom.Namespace;

import com.avetti.commerce.webservices.AttributeService_Orig;
import com.avetti.commerce.webservices.Inventory;
import com.avetti.commerce.webservices.InventoryService;
import com.avetti.commerce.webservices.ItemService;
import com.avetti.commerce.webservices.Items;

public class AttributesServiceTest {

    static {
        ProtocolSocketFactory easy = new EasySSLProtocolSocketFactory();
        Protocol protocol = new Protocol("https", easy, 443);
        Protocol.registerProtocol("https", protocol);
    }
    
    public void out(Object obj){
        System.out.println("-->"+obj);
    }
    
    public void findByCode()throws Exception{
        String vid="xx";
        String admin="xx";
        String pwd="xx";
        
        String url = "https://xx/preview/services/attributeservice";
        /*
        Service serviceModel = new ObjectServiceFactory().create(AttributeService.class);
        AttributeService service = (AttributeService) new XFireProxyFactory().create(
                serviceModel, url);

        Client client = Client.getInstance(service);
        client.addOutHandler(new VendorIdHandler(vid));
        client.addOutHandler(new UserPassHandler(admin, pwd));
        
        com.avetti.commerce.webservices.Attributes at=service.findByCode("EG");
        */
        
        Service serviceModel = new ObjectServiceFactory().create(AttributeService_Orig.class);
        AttributeService_Orig service = (AttributeService_Orig) new XFireProxyFactory().create(
                serviceModel, url);

        Client client = Client.getInstance(service);
        client.addOutHandler(new VendorIdHandler(vid));
        client.addOutHandler(new UserPassHandler(admin, pwd));
        
        com.avetti.commerce.domainmodel.Attributes at=service.findByCode("EG");
        
        out(at.getAttributeid());
        out(at.getDataname());
    }
    
    public void findAllInventory()throws Exception{
        String vid="20070723002";
        String admin="weisun_ws";
        String pwd="1234";
        
        String url = "http://localhost/t80dev/services/inventoryservice";
        
        Service serviceModel = new ObjectServiceFactory().create(InventoryService.class);
        InventoryService service = (InventoryService) new XFireProxyFactory().create(
                serviceModel, url);

        Client client = Client.getInstance(service);
        client.addOutHandler(new VendorIdHandler(vid));
        client.addOutHandler(new UserPassHandler(admin, pwd));
        
        List<Inventory> at=service.find();
        
        out(at.size());
        out(at.get(0).getInventoryid());
        out(at.get(0).getNextshipdate());
    }
    
    public Items findItem(Long iid)throws Exception{
    	String vid="20081028007";
        String admin="weisun";
        String pwd="1234";
        
        

        String url = "http://192.168.1.18/geiger/services/itemservice";

        Service serviceModel = new ObjectServiceFactory().create(ItemService.class);
        ItemService service = (ItemService) new XFireProxyFactory().create(
                serviceModel, url);

        Client client = Client.getInstance(service);
        client.addOutHandler(new VendorIdHandler(vid));
        client.addOutHandler(new UserPassHandler(admin, pwd));
        
        return service.find(iid);
    }

    public void test(String[] args) throws Exception {
        //findByCode();
        findAllInventory();
        
    }

    private class VendorIdHandler extends AbstractHandler {
        private String vendorId;

        VendorIdHandler(String vendorId) {
            super();
            this.vendorId = vendorId;
        }

        public String getVendorId() {
            return vendorId;
        }

        public void setVendorId(String vendorId) {
            this.vendorId = vendorId;
        }

        public void invoke(MessageContext context) throws Exception {
            final Namespace ns = Namespace.getNamespace("urn:commerce:vendor");
            Element header = context.getOutMessage().getHeader();

            if (header == null) {
                header = new Element("header");
                context.getOutMessage().setHeader(header);
            }

            Element vendor = new Element("VendorToken", ns);
            Element vendor_el = new Element("VendorId", ns);
            vendor_el.addContent(vendorId);
            vendor.addContent(vendor_el);
            header.addContent(vendor);
        }
    }

    private class UserPassHandler extends AbstractHandler {
        private String user;
        private String pass;

        UserPassHandler(String user, String pass) {
            super();
            this.user = user;
            this.pass = pass;
        }

        public void invoke(MessageContext context) throws Exception {
            final Namespace ns = Namespace.getNamespace("urn:commerce:authentication");
            Element header = context.getOutMessage().getHeader();

            if (header == null) {
                header = new Element("header");
                context.getOutMessage().setHeader(header);
            }

            Element root = new Element("AuthenticationToken", ns);
            Element username_el = new Element("Username", ns);
            Element password_el = new Element("Password", ns);

            username_el.addContent(user);
            password_el.addContent(pass);
            root.addContent(username_el);
            root.addContent(password_el);

            header.addContent(root);
        }
    }

    public static void main(String[] args) throws Exception {
        AttributesServiceTest cst=new AttributesServiceTest();
        cst.test(args);
    }
}
